package td.towers
{
	import flash.display.Graphics;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.TimerEvent;
	
	import td.Creep;
	import td.projectiles.*;
	
	/**
	 * RangerTower class
	 * @see 游侠
	 * 
	 * @author rayyee
	 */
	public class RangerTower extends Tower
	{
		
		/**
		 * Constructor method
		 * 
		 * @constructor
		 * @access public
		 * @param {MagicTD} mtd A reference to the current TD game
		 * @return Tower
		 */
		public function RangerTower(mtd :MagicTD,valueType:String=null)
		{
			if(valueType) this.type  = valueType;
			else this.type  = 'tower_8';
			this.power = 300;
			this.rate  = 0.5;
			this.range = 120;
			this.dead = 1.8;
			this.deadpro = 0.05;
			
			super(mtd,valueType);
		}
		
		
		
		/**
		 * 开火
		 * @param	e
		 */
		protected override function fire(e :TimerEvent):void {
			var t:Tower=this;
			try {//优化时 需去掉try
				var c :Creep  = this.game.obtainTarget(t.x, t.y, t.getRange());
				var b :Bullet = t.getNewBullet(c);
			
				this.game.bullets.push(b);
				this.game.bulletMC.addChild(b);
				
				//随着creep调整tower方向  set turret
				var diffX :int = Math.abs(t.x - c.x);
				var diffY :int = Math.abs(t.y - c.y);
				var dist :int = Math.round(Math.sqrt((diffX * diffX) + (diffY * diffY)));
				var sin_A:Number = diffY / dist;
				var asin_A:Number = Math.asin(sin_A);
				var cFrame:Number;
				if (c.x < t.x) {
					if (c.y < t.y) {
						cFrame = asin_A * 180 / Math.PI / 5;
					}else if (c.y > t.y) {
						cFrame = (360 - asin_A * 180 / Math.PI ) / 5;
					}else if (c.y == t.y) {
						cFrame = 1;
					}
				}else if (c.x > t.x) {
					if (c.y < t.y) {
						cFrame = (180 - asin_A * 180 / Math.PI) / 5;
					}else if (c.y > t.y) {
						cFrame = (180 + asin_A * 180 / Math.PI) / 5;
					}else if (c.y == t.y) {
						cFrame = 180 / 5;
					}
				}else if (c.x == t.x) {
					if (c.y < t.y) {
						cFrame = 90 / 5;
					}else if (c.y > t.y) {
						cFrame = 270 / 5;
					}
				}
				(t.towerMC as MovieClip).turret.gotoAndStop(Math.ceil(cFrame));
			}
			catch (e :Error)
			{
				// Do nothing, since we have no targets in range
			}
		}
		
		
		
		/**
		 * Creates a new bullet to be fired from this tower
		 * 
		 * @access public
		 * @param {Creep} t The target creep
		 * @return FireBullet
		 */
		public override function getNewBullet(t:Creep):Bullet
		{
			var b :FireBullet = new FireBullet(t);
			b.x = this.x;
			b.y = this.y;
			b.setPower(this.power);
			return b;
		}
	}
}